package com.kantboot.idsu.common.controller;


import com.kantboot.idsu.common.mapper.IdsuCommonMapper;
import com.kantboot.user.entity.CesUser;
import com.kantboot.user.service.CesUserService;
import com.kantboot.util.RestResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 通用增删查改
 */
@RestController
@RequestMapping("/idsu-common")
public class IdsuCommonController {
    @Resource
    IdsuCommonMapper idsuCommonMapper;
    @Resource
    private CesUserService cesUserService;

    @RequestMapping("/delete_by_id")
    public RestResult<Object> deleteById(@RequestParam("tableName") String tableName, @RequestParam("id") Long id){
        String table_name=tableNameTo_table_name(tableName);
        return RestResult.success(idsuCommonMapper.deleteById(table_name,id),"删除成功");
    }

    @RequestMapping("/find_by_id")
    public RestResult<Map<String,Object>> findById(@RequestParam("tableName") String tableName, @RequestParam("id") Long id){
        String table_name=tableNameTo_table_name(tableName);
        HashMap<String,Object> map=idsuCommonMapper.selectById(table_name,id);
        map.remove("is_delete");
        Set<String> strings = map.keySet();
        Iterator<String> it = strings.iterator();
        List<String> keys=new ArrayList<String>();
        while (it.hasNext()) {
            String str = it.next();
            keys.add(str);
        }

        for (int i = 0; i < keys.size(); i++) {
            map.put(lineToHump(keys.get(i)),map.remove(keys.get(i)));
        }

        return RestResult.success(map,"查询成功");
    }

    @RequestMapping("/find_by_mine")
    public RestResult<List<HashMap<String,Object>>> findByMine(@RequestParam("tableName") String tableName, HttpServletRequest request){
        String table_name=tableNameTo_table_name(tableName);
        CesUser cesUser=cesUserService.getUserInfo(request);
        List<HashMap<String,Object>> mapList=idsuCommonMapper.selectByUserId(table_name,cesUser.getId());
        for (int i = 0; i < mapList.size(); i++) {
            HashMap<String,Object> map=mapList.get(i);
            map.remove("is_delete");
            Set<String> strings = map.keySet();
            Iterator<String> it = strings.iterator();
            List<String> keys=new ArrayList<String>();
            while (it.hasNext()) {
                String str = it.next();
                keys.add(str);
            }
            for (int j = 0; j < keys.size(); j++) {
                map.put(lineToHump(keys.get(j)),map.remove(keys.get(j)));
            }
        }

        return RestResult.success(mapList,"查询成功");
    }

    public static String tableNameTo_table_name(String tableName){
        String[] split = tableName.split("");
        String str="";
        for (int i = 0; i < split.length; i++) {
            String str1=split[i];
            if(str1.charAt(0) >= 'A' && str1.charAt(0) <= 'Z'){
                str1="_"+str1;
            }
            str+=str1;
        }
        return str.toLowerCase().replaceAll("\\s","").replaceAll("\\*","");
    }


    /** 下划线转驼峰 */
    public static String lineToHump(String str) {
        Pattern linePattern = Pattern.compile("_(\\w)");
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

}